const PORT1 = "/api";
import {
	baseURL
} from "./env.js";
import {
	tokenBear
} from "../constants/apiConstants";
export default {
	showActionSheet() {
		//头像点击处理事件，使用uni.showActionSheet()调用菜单栏
		return new Promise((resolve, reject) => { // ✅ 返回 Promise
			uni.showActionSheet({
				itemList: ['拍照', '相册'],
				itemColor: '',
				//成功时回调
				success: (res) => {
					if (!res.cancel) {
						this.chooseImage(res.tapIndex)
							.then(imagePath => resolve(imagePath))
							.catch(error => reject("图片选择失败，请重试"));
					}
				},
				//失败时回调
				fail: function(err) {
					reject
				},
				complete: function(res) {}
			});
		});
	},
	checkedActionSheet(itemList) {
		//头像点击处理事件，使用uni.showActionSheet()调用菜单栏
		return new Promise((resolve, reject) => { // ✅ 返回 Promise
			uni.showActionSheet({
				itemList,
				itemColor: '',
				//成功时回调
				success: (res) => {
					console.log(res);
					resolve(res)
				},
				//失败时回调
				fail: function(err) {
					reject
				},
				complete: function(res) {}
			});
		});
	},

	chooseImage(tapIndex) {
		const sourceType = ['camera', 'album'];
		return new Promise((resolve, reject) => { // ✅ 返回 Promise
			uni.chooseImage({
				count: 1,
				sourceType: [sourceType[tapIndex]],
				async success(res) {
					resolve(res.tempFilePaths[0]); // ✅ 正确返回数据
				},
				fail: (err) => {
					reject(err);
					console.log('err: ', err);
					// #ifdef APP-PLUS
					if (err['code'] && err.code !== 0 && this.sourceTypeIndex === 2) {
						this.checkPermission(err.code);
					}
					// #endif
					// #ifdef MP
					if (err.errMsg.indexOf('cancel') !== '-1') {
						return;
					}
					uni.getSetting({
						success: (res) => {
							let authStatus = false;
							switch (this.sourceTypeIndex) {
								case 0:
									authStatus = res.authSetting['scope.camera'];
									break;
								case 1:
									authStatus = res.authSetting['scope.album'];
									break;
								case 2:
									authStatus = res.authSetting['scope.album'] && res
										.authSetting[
											'scope.camera'];
									break;
								default:
									break;
							}
							if (!authStatus) {
								uni.showModal({
									title: '授权失败',
									content: 'Hello uni-app需要从您的相机或相册获取图片，请在设置界面打开相关权限',
									success: (res) => {
										if (res.confirm) {
											uni.openSetting();
										}
									}
								});
							}
						}
					});
					// #endif
				}
			});
		});
	},
	uploadImage(e) {
		return new Promise((resolve, reject) => { // ✅ 返回 Promise
			uni.showLoading({
				title: "加载中"
			})
			uni.uploadFile({
				url: baseURL + PORT1 + "/Tools/UploadFiles",
				filePath: e,
				name: "file",
				header: {
					"content-type": "multipart/form-data; charset=utf-8",
					token: tokenBear,
				},
				success: (uploadFileRes) => {
					uni.hideLoading();
					var jsonData = JSON.parse(uploadFileRes.data);
					if (jsonData.code == 0) {
						resolve(jsonData.data.src);
					} else {
						console.log("jsonData", jsonData);
						reject("图片上传失败");
					}
				},
				fail: (err) => {
					uni.hideLoading();
					reject(err);
				},
			});
		});
	}
}